﻿@page "/dialogcard/{OrderID}"

@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inherits DbContextPage
@inject Radzen.DialogService dialogService

<RadzenStack Gap="1rem" Orientation="Orientation.Vertical" JustifyContent="JustifyContent.SpaceBetween" Style="height: 100%;">
    <RadzenStack>
    <RadzenRow>
        <RadzenColumn Size="12" SizeMD="6" class="rz-p-4 rz-border-radius-1" Style="border: var(--rz-grid-cell-border)">
            <RadzenText TextStyle="TextStyle.Subtitle1">Contact</RadzenText>
            <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center">
                <RadzenImage Path="@order.Employee?.Photo" Style="width: 80px; height: 80px; border-radius: 50%" AlternateText="@(order.Employee?.FirstName + " " + order.Employee?.LastName)" />
                <RadzenStack Gap="0" class="rz-text-truncate">
                    <RadzenText TextStyle="TextStyle.Overline" class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">Employee</RadzenText>
                    <RadzenText TextStyle="TextStyle.Body1" class="rz-text-truncate"><b>@(order?.Employee?.FirstName + " " + order?.Employee?.LastName)</b></RadzenText>
                    <RadzenText TextStyle="TextStyle.Overline" class="rz-mt-4 rz-mb-0" Style="color: var(--rz-text-tertiary-color);">Company</RadzenText>
                    <RadzenText TextStyle="TextStyle.Body1" class="rz-text-truncate"><b>@order.Customer?.CompanyName</b></RadzenText>
                </RadzenStack>
            </RadzenStack>
        </RadzenColumn>
        <RadzenColumn Size="12" SizeMD="6" class="rz-p-4 rz-border-radius-1" Style="border: var(--rz-grid-cell-border)">
            <RadzenText TextStyle="TextStyle.Subtitle1">Delivery Information</RadzenText>
            <RadzenStack Gap="0" class="rz-text-truncate">
                <RadzenText TextStyle="TextStyle.Overline" class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">Address</RadzenText>
                <RadzenText TextStyle="TextStyle.Body1" class="rz-text-truncate"><b>@(order.ShipAddress), @(order.ShipCity), @(order.ShipCountry)</b></RadzenText>
                <RadzenText TextStyle="TextStyle.Overline" class="rz-mt-2 rz-my-0" Style="color: var(--rz-text-tertiary-color);">Ship name</RadzenText>
                <RadzenText TextStyle="TextStyle.Body1" class="rz-text-truncate"><b>@(order.ShipName)</b></RadzenText>
            </RadzenStack>
        </RadzenColumn>
    </RadzenRow>
    <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.SpaceBetween" AlignItems="AlignItems.Center">
        <RadzenText TextStyle="TextStyle.Subtitle1" class="rz-m-0">Order @OrderID Details</RadzenText>
        <RadzenBadge BadgeStyle="BadgeStyle.Secondary" Text=@($"{String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", order.Freight)}")/>
    </RadzenStack>
    <RadzenDataGrid AllowFiltering="true" AllowPaging="true" AllowSorting="true" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                Data="@(orderDetails)" ColumnWidth="200px" Density="Density.Compact" AllowAlternatingRows="false">
        <Columns>
            <RadzenDataGridColumn Property="@nameof(OrderDetail.Quantity)" Title="Quantity" />
            <RadzenDataGridColumn Property="Order.OrderDate" Title="Order Date" FormatString="{0:d}" />
            <RadzenDataGridColumn Property="@nameof(OrderDetail.Discount)" Title="Discount" FormatString="{0:P}" />
        </Columns>
    </RadzenDataGrid>
    </RadzenStack>
    <RadzenStack Visible="@ShowClose" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Gap="0.5rem">
        <RadzenButton Click="@((args) => dialogService.Close(true))" Variant="Variant.Flat" Text="Close" Style="width: 120px" />
    </RadzenStack>
</RadzenStack>
@code {
    [Parameter] public int OrderID { get; set; }
    [Parameter] public bool ShowClose { get; set; } = true;

    Order order;
    IEnumerable<OrderDetail> orderDetails;

    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();

        order = dbContext.Orders.Where(o => o.OrderID == OrderID)
                          .Include("Customer")
                          .Include("Employee").FirstOrDefault();

        orderDetails = dbContext.OrderDetails.Where(o => o.OrderID == OrderID).Include("Order").ToList();
    }
}
